package com.rw.base.http;

import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.LogUtils;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author: ZengZhenHua
 * @date: 2018/12/17
 * @email: cengzhenghua@163.com
 */
public class RetrofitManager {

    private static final long DEFAULT_TIME_OUT = 10;
    private static String BASE_URL = "http://39.105.48.179/phone/demo/";
//    private static String BASE_URL = "http://192.168.46.109/phone/demo/";//测试地址
    private volatile static RetrofitManager mInstance = null;

    private Retrofit mRetrofit;

    private RetrofitManager(){
        mRetrofit = newRetrofit();
    }

    public static RetrofitManager getInstance() {
        if (mInstance == null ){
            synchronized (RetrofitManager.class){
                if (mInstance == null ){
                    mInstance = new RetrofitManager();
                }
            }
        }
        return mInstance;
    }



    /**
     * 获取对应的Service
     * @param service Service 的 class
     * @param <T>
     * @return
     */
    public <T> T create(Class<T> service){
        return mRetrofit.create(service);
    }

    private Retrofit newRetrofit() {
        Retrofit retrofit = new Retrofit.Builder()
                .client(newOkHttpClient())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BASE_URL)
                .build();
        return retrofit;
    }

    private OkHttpClient newOkHttpClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS);//连接 超时时间
        builder.writeTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS);//写操作 超时时间
        builder.readTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS);//读操作 超时时间
        builder.retryOnConnectionFailure(true);//错误重连

        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                if (TextUtils.isEmpty(message)) return;
                String s = message.substring(0, 1);
                //如果收到响应是json才打印
                if ("{".equals(s) || "[".equals(s)) {
                    LogUtils.iTag("收到响应: ", message);
                }
//                LogUtils.eTag("响应", message);
                Log.e("响应",message);
//                Log.d(TAG,"HttpLoggingInterceptor");
            }
        }).setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(logInterceptor);
        // 添加公共参数拦截器
        /*BasicParamsInterceptor basicParamsInterceptor = new BasicParamsInterceptor.Builder()
                .addHeaderParam("userName", "")//添加公共参数
                .addHeaderParam("device", "")
                .build();
        builder.addInterceptor(basicParamsInterceptor);
*/
        return builder.build();
    }
}
